from fastapi import FastAPI
from starlette.middleware.base import BaseHTTPMiddleware

from routers import users
from middlewares.dbs import create_session_middleware

app = FastAPI()
app.include_router(users.router)

app.add_middleware(BaseHTTPMiddleware, dispatch=create_session_middleware)

# 中间件装饰器写法的调用
# @app.middleware('http')
# def middleware(
#         self,
#         middleware_type: Annotated[
#             str,
#             Doc(
#                 """
#                 The type of middleware. Currently only supports `http`.
#                 """
#             ),
#         ],
# ) -> Callable[[DecoratedCallable], DecoratedCallable]:
#
#     def decorator(func: DecoratedCallable) -> DecoratedCallable:
#         self.add_middleware(BaseHTTPMiddleware, dispatch=func)
#         return func
#
#     return decorator

# 核心代码就是 self.add_middleware(BaseHTTPMiddleware, dispatch=func)

@app.get("/")
async def root():
    return {"message": "Hello World"}


@app.get("/hello/{name}")
async def say_hello(name: str):
    return {"message": f"Hello {name}"}
